package ui.Config;

import context.ClientFrameContext;
import nettyclient.NettyInit;
import ui.Frame.AdminFrame;
import ui.Frame.JFrameGame;
import ui.Frame.WaitFrame;
import util.FrameUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Hujf
 * @title: LoginConfig
 * @date 2021/2/14 0014下午 5:06
 * @description: TODO
 */
public class LoginConfig extends JFrame {
    private NettyInit nettyInit;
    private JFrameGame frame;
    public LoginConfig(WaitFrame wFrame, JFrameGame frame) {
        wFrame.setVisible(false);
        this.frame = frame;
        this.setTitle("用户登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600,600);
        this.setLayout(new BorderLayout());
        FrameUtil.setFrameCenter(this);
        // 添加登录面板
        this.add(this.createLoginPanel(), BorderLayout.CENTER);
        this.setResizable(true);
        this.setVisible(true);
    }

    private JLabel  tabs;
    private JLabel createLoginPanel() {
        this.tabs = new JLabel();
        this.tabs.setOpaque(false);
        JLabel jl=new JLabel("用户名：");
        jl.setBounds(100, 100, 200, 30);
        //用户名框
        final JTextField name=new JTextField();
        name.setBounds(180, 100, 150, 30);
        this.tabs.add(jl);
        this.tabs.add(name);


        JLabel j2=new JLabel("密码：");
        j2.setBounds(100, 150, 200, 30);
        //密码框
        final JPasswordField  password=new JPasswordField();
        password.setBounds(180, 150, 150, 30);
        this.tabs.add(j2);
        this.tabs.add(password);

        //添加一个确定按钮
        JButton jb=new JButton("登录");
        //为确定按钮添加监听事件
        jb.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {

                if(name.getText().trim().length()==0||new String(password.getPassword()).trim().length()==0){
                    JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
                    return;
                }
                if(check(name.getText(),new String(password.getPassword()))){
                    nettyInit =new NettyInit();
                    AdminFrame sadas=new AdminFrame(nettyInit);
                    nettyInit.frameInit(name.getText(),new String(password.getPassword()),2,sadas);
                    JOptionPane.showMessageDialog(null, "登录成功。。。跳转中");
                    setVisible(false);
                    //new AdminFrame(nettyInit);
                    System.out.println(NettyInit.socketChannel.isActive());
                    ClientFrameContext context = new ClientFrameContext(sadas);
                    context.init(name.getText());
                }
                else{
                    JOptionPane.showMessageDialog(null, "用户名或密码错误");
                }
            }
        });
        //设置确定按钮的宽，高，x值，y值
        jb.setBounds(180, 200, 60, 18);
        //将确定按钮添加到cp容器中
        this.tabs.add(jb);

        //添加一个确定按钮
        JButton jb2=new JButton("注册");
        //为确定按钮添加监听事件
        jb2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {

                if(name.getText().trim().length()==0||new String(password.getPassword()).trim().length()==0){
                    JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
                    return;
                }
                if(check2(name.getText(),new String(password.getPassword()))){
                    nettyInit =new NettyInit();
                    nettyInit.init(name.getText(),new String(password.getPassword()),1,null);
                    JOptionPane.showMessageDialog(null, "注册成功");
                }
                else{
                    JOptionPane.showMessageDialog(null, "该用户名已经被注册");
                }
            }

        });
        //设置确定按钮的宽，高，x值，y值
        jb2.setBounds(280, 200, 60, 18);
        //将确定按钮添加到cp容器中
        this.tabs.add(jb2);

        return tabs;
    }
    //调用微服务
    private boolean check2(String text, String s) {
        return true;
    }

    //调用微服务
    private boolean check(String text, String s) {
        return true;
    }
}
